from django.db import models

VMTYPE_CHOICES = (
    ('VMSRV', 'VMWare Server 1.X'),
    ('VMESX2', 'VMWare ESX Server 2.X)'),
    ('VMESX3', 'VMWare ESX Server 3.X)'),
    ('XEN3', 'Xen 3.X'),
)

VMOSGUEST_CHOICES = (
    ('WIN32', 'Windows'),
    ('Linux', 'Linux'),
    ('BSD', 'BSD'),
)

VMCOPYMODE_CHOICES = (
    ('NFS', 'NFS'),
    ('SCP', 'SCP'),
)

JOBTYPE_CHOICES = (
    ('BACKUP', 'Backup'),
    ('RECOVER', 'Recover'),
    ('MOVE', 'Move'),
    ('DUP', 'Duplicate'),
)

JOBMODE_CHOICES = (
    ('HOT', 'HOT (minimize downtime)'),
    ('COLD', 'COLD (VM will be stopped'),
    ('SUSPEND', 'SUSPEND (VM will be suspended'),
)

JOBSTATE_CHOICES = (
    ('NEW', 'New'),
    ('READY', 'Ready to go!'),
    ('PERFORMING', 'Performing'),
    ('SUCCESS', 'Successfully done'),
    ('ERROR', 'Errors'),
)

RECOVER_CHOICES = (
    ('REP', 'Replace the actual running VM with the backup'),
    ('CPY', 'Recover on the same VMHost with a new name (do not erase)'),
    ('MVE', 'Recover on an other VMHost'),
)

LOGLEVEL_CHOICES = (
    ('INFO', 'INFO'),
    ('WARN', 'WARNING'),
    ('CRIT', 'CRITICAL'),
    ('DEBUG', 'DEBUG'),                    
)

BACKUP_START_METHOD_CHOICES = (
    ('EVD', 'Everyday'),
    ('WE', 'Only during WeekEnds')
)

JOBPOOL_STATES_CHOICES = (
    ('NEW', 'Never Run'),
    ('RDY', 'Ready to go'),
    ('PRF', 'Performing'),
    ('PWE', 'Performing with error'),
    ('SUC', 'Success'),
    ('DWE', 'Done with error'),
)

HOURS_CHOICES = (('0', '00'), ('1', '01'), ('2', '02'), ('3', '03'), ('4', '04'),
('5', '05'), ('6', '06'), ('7', '07'), ('8', '08'), ('9', '09'), ('10', '10'), 
('11', '11'), ('12', '12'), ('13', '13'), ('14', '14'), ('15', '15'), ('16', '16'),
('17', '17'), ('18', '18'), ('19', '19'), ('20', '20'), ('21', '21'), ('22', '22'),
('23', '23'),
)

class LocalStorage(models.Model):
    name = models.CharField(maxlength=80)
    path = models.CharField(maxlength=256)
    remotepath = models.CharField(maxlength=256)
    size = models.IntegerField(null=True, blank=True)
    avail = models.IntegerField(null=True, blank=True)
    
    def __str__(self):
        return self.name
    
    class Admin:
        pass

class VMHOST(models.Model):
    name = models.CharField(maxlength=80)
    ipaddress = models.CharField(maxlength=80)
    type = models.CharField(maxlength=10, choices=VMTYPE_CHOICES)
    copyMode = models.CharField(maxlength=3, choices=VMCOPYMODE_CHOICES)
    NFSRootPath = models.CharField(maxlength=254)
    vm_root_path = models.CharField(maxlength=254)
    backup_storage = models.ForeignKey(LocalStorage)
    comments = models.TextField(maxlength=100, null=True, blank=True)
    
    def __str__(self):
        return self.name
    
    class Admin:
        pass

class VM(models.Model):
    name = models.CharField(maxlength=80)
    path = models.CharField(maxlength=254)
    configFileName = models.CharField(maxlength=80)
    osguest = models.CharField(maxlength=20, choices=VMOSGUEST_CHOICES)
    vmhost = models.ForeignKey(VMHOST)
    alldisks = models.BooleanField(default=True)
    comments = models.TextField(maxlength=100, null=True, blank=True)
 
    def __str__(self):
        return self.name
    
    class Admin:
        pass  

class VMDisk(models.Model):
    name = models.CharField(maxlength=80)
    configFileName = models.CharField(maxlength=256)
    sizeInGB = models.CharField(maxlength=5, null=True, blank=True)
    vm = models.ForeignKey(VM)
 
    def __str__(self):
        return self.name

    class Admin:
        pass

class OVIMSettings(models.Model):
    vmware_cmd_path = models.CharField(maxlength=256, null=True, blank=True, default='/usr/bin/vmware-cmd')
    vmware_run_path = models.CharField(maxlength=256, null=True, blank=True, default='/usr/bin/vmrun')
    perform_rotation_hr = models.CharField(maxlength=2, choices=HOURS_CHOICES, default='00')
    main_store = models.ForeignKey(LocalStorage, related_name='mainstore', null=True, blank=True,)
    second_store = models.ForeignKey(LocalStorage, null=True, blank=True, related_name='secstore')
    recovery_store = models.ForeignKey(LocalStorage, null=True, blank=True, related_name='recostore')
    
    class Admin:
        pass

class BackupJobPool(models.Model):
    name = models.CharField(maxlength=80)
    cur_taskid = models.PositiveIntegerField(default=0)
    comments = models.TextField(maxlength=100, null=True, blank=True)
    backupmode = models.CharField(maxlength=10, choices=JOBMODE_CHOICES)
    state = models.CharField(maxlength=10, choices=JOBPOOL_STATES_CHOICES, default='NEW')
    startat = models.CharField(maxlength=2, choices=HOURS_CHOICES)
    startdate = models.DateTimeField(null=True, blank=True)
    enddate = models.DateTimeField(null=True, blank=True)
    startmethod = models.CharField(maxlength=4, choices=BACKUP_START_METHOD_CHOICES)
    timeoutH = models.IntegerField()
    vmlist = models.ManyToManyField(VM, null=True, blank=True)
    lastrun = models.DateTimeField(null=True, blank=True)
    
    def __str__(self):
        return self.name
    
    class Admin:
        pass

class Jobs(models.Model):
    vm = models.ForeignKey(VM)
    type = models.CharField(maxlength=10, choices=JOBTYPE_CHOICES)
    mode = models.CharField(maxlength=10, choices=JOBMODE_CHOICES)
    state = models.CharField(maxlength=10, choices=JOBSTATE_CHOICES)
    storage = models.ForeignKey(LocalStorage, null=True, blank=True)
    reco_mode = models.CharField(maxlength=3, choices=RECOVER_CHOICES, null=True, blank=True)
    reco_newname = models.CharField(maxlength=80, null=True, blank=True)
    subdate = models.DateTimeField(auto_now_add=True)
    startdate = models.DateTimeField(null=True, blank=True)
    enddate = models.DateTimeField(null=True, blank=True)
    backupjobpool = models.ForeignKey(BackupJobPool, null=True, blank=True)
    joppool_id = models.PositiveIntegerField(blank=True, null=True, default=0)
    job_pid = models.CharField(maxlength=10, null=True, blank=True)
    
    class Admin:
        pass

class Log(models.Model):
    loglevel = models.CharField(maxlength=5, choices=LOGLEVEL_CHOICES)
    component = models.CharField(maxlength=20, null=True, blank=True)
    date = models.DateTimeField()
    jobid = models.IntegerField(null=True, blank=True)
    log = models.TextField(null=True, blank=True)
    
    class Admin:
        pass